<template>
  <div class="Zlgl">
    <!-- 第一行 -->
    <el-row>
      <el-col class="header_col">
        <div class="header_title">资料管理</div>
      </el-col>
    </el-row>
    <!-- 第二行 -->
    <el-row>
      <el-col class="header_col">
        <el-button
          class="addInformation_btn"
          type="primary"
          round
          size="mediu"
          @click="addInfo"
          >添加资料</el-button
        >
      </el-col>
      <!-- @click="addInformation = true" -->
    </el-row>
    <!-- 第三行 -->
    <el-row>
      <el-col>
        <div class="content_search">
          <el-form ref="form" :model="searchData" label-width="80px">
            <el-form-item label="资料名称">
              <el-input
                v-model="searchData.nameInfo"
                placeholder="请输入资料关键词"
                clearable
                size="small"
              ></el-input>
            </el-form-item>
            <el-form-item label="装备型号" size="small">
              <el-select
                v-model="searchData.equipment_type"
                placeholder="请选择装备型号"
                clearable
              >
                <el-option
                  v-for="(v, i) in equipment_typeData"
                  :key="i"
                  :label="v"
                  :value="v"
                ></el-option>
              </el-select>
            </el-form-item>
            <!--    <el-form-item label="专业类别" size="small">
              <el-select
                v-model="searchData.storage_area"
                clearable
                placeholder="请选择专业类别"
              >
                <el-option label="全部" value="全部"></el-option>
                <el-option label="装备使用" value="装备使用"></el-option>
                <el-option label="装备制度" value="装备制度"></el-option>
              </el-select>
            </el-form-item> -->
            <!-- 按钮组 -->
            <div class="btn_type">
              <el-button
                type="info"
                icon="el-icon-refresh-right"
                size="mediu"
                @click="onReset"
                >重置</el-button
              >
              <el-button
                type="primary"
                icon="el-icon-zoom-in"
                size="mediu"
                @click="onSearch"
                >查询</el-button
              >
            </div>
          </el-form>
        </div>
      </el-col>
    </el-row>
    <!-- 表格数据 -->
    <el-table :data="ZlglData" style="width: 100%">
      <el-table-column prop="dataName" label="资料名称" align="center">
      </el-table-column>
      <el-table-column
        prop="equipmentData.categoryName"
        label="资料类型"
        align="center"
      >
        <!-- <template slot-scope="scope">
          <span>{{
            scope.row.equipmentData.categoryId == 1
              ? "维修中"
              : scope.row.equipmentData.categoryId == 2
              ? "待维修"
              : scope.row.equipmentData.categoryId == 3
              ? "维修完成"
              : ""
          }}</span>
        </template> -->
      </el-table-column>

      <el-table-column prop="equipmentModel" label="装备型号" align="center">
      </el-table-column>
      <!--       <el-table-column
        label="上传时间"
        align="center"
      >
        <template slot-scope="scope">{{
          scope.row.fileUploadTime | formatTimer
        }}</template>
      </el-table-column> -->
      <el-table-column
        prop="dataDesc"
        label="资料说明"
        align="center"
        show-overflow-tooltip
      >
      </el-table-column>
      <el-table-column label="操作" align="center">
        <template slot-scope="scope">
          <el-button
            class="view_btn"
            type="primary"
            icon="el-icon-view"
            @click="openDetail(scope.row)"
          >
          </el-button>
          <el-button
            class="download_btn"
            type="primary"
            icon="el-icon-download"
            @click="downloadInfo(scope.row)"
          >
            <!-- <a @click="downloadFile(scope.row.attachName,scope.row.attachUrl)"></a> -->
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 页码 -->
    <el-col :span="24">
      <div class="grid_content">
        <span class="page_total"
          >共{{ Math.ceil(total / zlgl_limit) }}页/{{ total }}条数据</span
        >
        <el-pagination
          @current-change="zlgl_changePage"
          layout="prev, pager, next, jumper"
          :page-size="zlgl_limit"
          :total="total"
        >
        </el-pagination>
        <span class="go_page">跳转</span>
      </div>
    </el-col>
  </div>
</template>
<script>
export default {
  data: function () {
    return {
      search_input: "",
      searchData: {
        nameInfo: "",
        equipment_type: "",
      },
      ZlglData: [],
      equipment_typeData: [],
      zlgl_page: 1,
      zlgl_limit: 5,
      total: 0,
    };
  },
  methods: {
    getZlglData: function () {
      this.$axios({
        url: "/data/list",
        method: "post",
        params: {
          page: this.zlgl_page,
          limit: this.zlgl_limit,
        },
        data: {
          dataName: this.searchData.nameInfo,
          equipmentModel: this.searchData.equipment_type,
        },
      }).then((res) => {
        // console.log("资料管理", res.data);
        if (res.data.code == 0) {
          this.ZlglData = res.data.data;
          this.total = res.data.total;
          // this.$message.success("数据加载成功！");
        } else {
          this.$message.error("数据加载失败！");
        }
      });
    },
    // 添加资料信息
    addInfo: function () {
      this.$router.push("/home/Zlgl_add");
    },
    // 搜索
    onSearch: function () {
      // console.log("姓名", this.searchData.nameInfo);
      // console.log("类型", this.searchData.equipment_type);
      this.getZlglData();
    },
    onReset: function () {
      this.searchData.equipment_type = "";
      this.searchData.nameInfo = "";
      this.zlgl_page = 1;
      this.getZlglData();
    },
    // 查看详情
    openDetail: function (row) {
      // 查看详情
      let zlgl_data = row;
      this.$store.state.tjy_vuex.zlgl_data = zlgl_data;
      sessionStorage.setItem("zlglInfo", JSON.stringify(zlgl_data));
      // console.log(sessionStorage.getItem( 'zlglInfo'))
      this.$router.push("/home/Zlgl_detail");
    },
    // 下载
    downloadInfo: function (downloadData) {
      this.$axios({
        url: "/download",
        responseType: "blob", //设置返回类型
        params: {
          fileName:downloadData.fileAddress,
        }
      }).then((res) => {
        // console.log(res)
        const link = document.createElement("a");
        try {
          let blob = new Blob([res.data], {
            type: "application/vnd.ms-excel",
          });
          let _fileName = res.headers["content-disposition"]
            .split(";")[1]
            .split("=")[1]; //文件名，中文无法解析的时候会显示 _(下划线),生产环境获取不到
          link.style.display = "none";
          // 兼容不同浏览器的URL对象
          const url = window.URL || window.webkitURL || window.moxURL;

          link.href = url.createObjectURL(blob);

          link.setAttribute(
            "download",
            _fileName.substring(_fileName.lastIndexOf("_") + 1)
          );
          document.body.appendChild(link);
          link.click();
          document.body.removeChild(link);
          url.revokeObjectURL(link.href); //销毁url对象
        } catch (e) {
          console.log("下载的文件出错", e);
        }
      });
    },
    // 装备类型下拉数据
    getSelectEquipmentModel: function () {
      this.$axios({
        url: "/data/selectEquipmentModel",
        method: "post",
      }).then((res) => {
        // console.log(res)
        if (res.data.code == 0) {
          this.equipment_typeData = res.data.data;
        } else {
          this.$message.error("下拉框数据加载失败！");
        }
      });
    },
    // 页码
    zlgl_changePage: function (page) {
      this.zlgl_page = page;
      this.getZlglData();
    },
  },

  // 日期时间格式化过滤器
  filters: {
    formatTimer: function (time) {
      let date = new Date(time);
      let y = date.getFullYear();
      let MM = date.getMonth() + 1;
      MM = MM < 10 ? "0" + MM : MM;
      let d = date.getDate();
      d = d < 10 ? "0" + d : d;
      let h = date.getHours();
      h = h < 10 ? "0" + h : h;
      let m = date.getMinutes();
      m = m < 10 ? "0" + m : m;
      let s = date.getSeconds();
      s = s < 10 ? "0" + s : s;
      // return y + "-" + MM + "-" + d + " " + h + ":" + m;
      return y + "-" + MM + "-" + d + " " + h + ":" + m;
    },
  },
  mounted: function () {
    this.getZlglData();
    this.getSelectEquipmentModel();
  },
};
</script>


<style lang="less" scoped>
.Zlgl {
  width: 100%;
  height: 100%;

  .header_col {
    margin-top: 20px;
    padding-left: 30px;
    min-width: 1440px;
  }

  .header_title {
    height: 40px;
    width: 100%;
    font-size: 24px;
    line-height: 40px;
    color: #344563;
    font-weight: bolder;
  }
  .addInformation_btn {
    width: 110px;
    height: 40px;
    color: #fff;
  }

  .content_search {
    margin-top: 20px;
    height: 60px;
    margin-left: 30px;
    min-width: 1350px;
    width: calc(80% - 30px);
    background: #ffffff;
    // background: yellow;
    font-size: 16px;
    line-height: 60px;
    color: #344563;
    font-weight: bolder;
    .el-form {
      height: 50px;
      line-height: 50px;
    }
    .el-form-item {
      width: 300px;
      height: 40px;
      display: inline-block;
    }
    // 输入框
    .el-input {
      width: 200px;
    }

    .btn_type {
      width: 340px;
      height: 41px;
      line-height: 41px;
      display: inline-block;
      margin-left: 350px;
    }

    .el-button {
      padding: 0;
      text-align: center;
      width: 80px;
      height: 40px;
      line-height: 40px;
      margin-left: 30px;
    }
    .el-icon-search {
      font-size: 28px;
    }
    .search_btn {
      background: #64c8bc;
      border: 1px solid #64c8bc;
    }
    .el-icon-refresh-right {
      font-size: 28px;
    }
    .reset_btn {
      background: #61a4e4;
      border: 1px solid #61a4e4;
    }

    .del_btn {
      font-size: 14px;
      color: #ffc5c5;
      background: #fef3f3;
      border-color: #fef3f3;
    }

    .edit_btn {
      font-size: 14px;
      color: #5fa4e2;
      font-weight: bolder;
      background: #eef6fc;
      border-color: #eef6fc;
    }
  }

  // 表格
  .el-table {
    margin-top: 20px;
    padding: 20px 25px 0px 10px;
    .download_btn {
      font-size: 20px;
      color: #ffc6c0;
      background: #fef3f3;
      border-color: #fef3f3;
    }

    .view_btn {
      font-size: 20px;
      color: #5fa4e2;
      font-weight: bolder;
      background: #eef6fc;
      border-color: #eef6fc;
    }
  }

  // 底部页码
  .grid_content {
    // background: #61a4e4;
    line-height: 40px;
    position: relative;
    margin-top: 20px;

    .el-pagination {
      border-radius: 10px;
      position: absolute;
      top: 5px;
      right: 90px;
      color: #a1a1a1;
    }
    .go_page {
      font-size: 14px;
      position: absolute;
      top: 0;
      right: 50px;
      color: #7161c5;
      &:hover {
        cursor: pointer;
      }
    }
    .page_total {
      font-size: 14px;
      position: absolute;
      top: 0;
      right: 620px;
      color: #a8afbc;
    }
  }
}
</style>
